package entity

import (
	"context"
	"time"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/util/structure"
	"github.com/jinzhu/gorm"
)

// GetOrderDB 获取order存储
func GetOrderDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return GetDBWithModel(ctx, defDB, new(Order))
}

// SchemaOrder order对象
type SchemaOrder schema.Order

// ToOrder 转换为order实体
func (a SchemaOrder) ToOrder() *Order {
	item := new(Order)
	structure.Copy(a, item)
	return item
}

// Order order实体
type Order struct {
	ID           string     `gorm:"column:id;primary_key;size:36;"`
	UserId       string     `gorm:"size:36;column:user_id;"`        // 用户ID
	OutOrderId   string     `gorm:"size:128;column:out_order_id;"`  // 外部订单ID
	InterOrderId string     `gorm:"size:64;column:inter_order_id;"` // 内部订单ID
	PayType      int        `gorm:"column:pay_type;size:11;"`       // 支付类型(1:微信支付 2:支付宝)
	Type         int        `gorm:"column:type;size:11;"`           // 类型(1:奉献 2:惩罚)
	Money        int        `gorm:"column:money;size:11;"`          // 金额
	Memo         string     `gorm:"size:512;column:memo;"`          // 支付说明
	Status       int        `gorm:"column:status;size:11;"`         // 支付状态(1:支付中 2:支付成功 3:支付失败)
	Creator      string     `gorm:"size:36;index;column:creator;"`  // 创建者
	CreatedAt    time.Time  `gorm:"column:created_at;index;"`
	UpdatedAt    time.Time  `gorm:"column:updated_at;index;"`
	DeletedAt    *time.Time `gorm:"column:deleted_at;index;"`
}

// ToSchemaOrder 转换为order对象
func (a Order) ToSchemaOrder() *schema.Order {
	item := new(schema.Order)
	structure.Copy(a, item)
	return item
}

// Orders order列表
type Orders []*Order

// ToSchemaOrders 转换为order对象列表
func (a Orders) ToSchemaOrders() []*schema.Order {
	list := make([]*schema.Order, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaOrder()
	}
	return list
}
